package top.hkyzf.onekeycommand.util;

import io.netty.util.concurrent.DefaultThreadFactory;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * description: 工具类
 * @author zhufeng
 * @date 2022/8/4 17:24
 * @version V1.0
 */
public class Utils {

    /**
     * 当前 MOD 的唯一标识
     */
    public static final String MOD_ID = "onekeycommand";

    /**
     * 用于将日志写入控制台和文件
     * 使用 modid 是最佳实践，可以区分是哪个 Mod 写入的日志
     */
    public static final Logger LOGGER = LoggerFactory.getLogger(Utils.MOD_ID);

    /**
     * 执行工作的线程池
     */
    private static final ThreadPoolExecutor GAME_THREAD_POOL =
            new ThreadPoolExecutor(5, 10, 60L, TimeUnit.SECONDS,
            new SynchronousQueue<>(),
            new DefaultThreadFactory("OneKeyCommand"),
            new ThreadPoolExecutor.CallerRunsPolicy());

    /**
     * 使用新的线程执行工作
     * @return 线程池
     */
    public static Executor getExecutor() {
        return GAME_THREAD_POOL;
    }

    // 创建传送键映射
    public static final KeyBinding TRANSPORT_KEY = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.transport",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_GRAVE_ACCENT,
            "key.category." + Utils.MOD_ID));
    // 创建挂机键映射
    public static final KeyBinding ON_HOOK_KEY = KeyBindingHelper.registerKeyBinding(new KeyBinding(
            "key.onhook",
            InputUtil.Type.KEYSYM,
            GLFW.GLFW_KEY_F10,
            "key.category." + Utils.MOD_ID));

    /**
     * 获取一个 [min, max] 范围内的随机数
     * @param min 最小值
     * @param max 最大值
     * @return 随机数
     */
    public static int getRandom(int min, int max) {
        Random random = new Random();
        return random.nextInt(max)%(max-min+1) + min;
    }
}

